#include "DirectoryScanner.h"

vector<string> DirectoryScanner::scan(const string& dir_str){
    // 该目录下所有的普通文件名称
    vector<string> files;

    const char *dir_path = dir_str.c_str();

    DIR *dir = opendir(dir_path);
    if(dir == nullptr){
        cerr << "open dir failed, check if your dir path is vaild." << endl;
        exit(1);
    }

    struct dirent *dir_info;
    
    // 遍历目录下的所有文件
    while((dir_info = readdir(dir)) != nullptr){
        const char *file_name = dir_info->d_name;
        // =过滤掉 '.' 和 '..'
        if (strcmp(file_name, ".") == 0 || strcmp(file_name, "..") == 0){
            continue;
        }

        if (dir_info->d_type == DT_REG){
            // 判断文件类型是否为文件
            files.push_back(file_name);
        }else{
            continue;
        }
    }

    // 关闭目录流
    closedir(dir);

    return files;
}